package com.tbit.api;

import com.tbit.common.entity.view.Result;
import com.tbit.manager.api.RideCardApi;
import com.tbit.manager.api.dto.PackageDelayDTO;
import com.tbit.manager.api.dto.RideCardDTO;
import com.tbit.manager.api.dto.UserMemberDTO;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.RideCard;
import com.tbit.uqbike.object.pojo.UserMember;
import com.tbit.uqbike.service.business.RideCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 区域礼品卡用户余额实现
 *
 * @author ：zhangtianyou
 * @since 2024/11/22
 */
@RestController
@Slf4j
public class RideCardApiImpl implements RideCardApi {

    @Autowired
    private RideCardService rideCardService;

    @Override
    public List<UserMemberDTO> userRideCardPage(Integer userId, Integer pageSize, Integer pageNumber) {
        List<UserMember> userMemberList = rideCardService.userRideCardPage(userId, pageSize, pageNumber);
        List<UserMemberDTO> userMemberDTOList = new ArrayList<>();
        for (UserMember userMember : userMemberList) {
            UserMemberDTO userMemberDTO = new UserMemberDTO();
            BeanUtils.copyProperties(userMember, userMemberDTO);
            if (userMember.getUserType() == 0){
                userMemberDTO.setRestNum(userMember.getMaxRide() - userMember.getUseCount());
            }else {
                userMemberDTO.setRestNum(userMember.getMaxRide() - userMember.getCurrentNum());
            }
            userMemberDTOList.add(userMemberDTO);
        }
        return userMemberDTOList;
    }

    @Override
    public Result<String> modifyExpireTime(PackageDelayDTO delayDTO) {
        com.tbit.uqbike.object.pojo.dto.PackageDelayDTO packageDelayDTO = new com.tbit.uqbike.object.pojo.dto.PackageDelayDTO();
        BeanUtils.copyProperties(delayDTO, packageDelayDTO);
        packageDelayDTO.setDelayTime(delayDTO.getDelayTime().getTime());
        Result<String> resultManager = rideCardService.modifyRideCardExpireTime(packageDelayDTO);
        Result<String> result = Result.success(resultManager.getData());
        if (resultManager.unSuccess()) {
            result = Result.error(resultManager.getMsg());
        }
        return result;
    }

    @Override
    public Result<String> userRideCardEdit(RideCardDTO rideCardDTO) {
        UserMember userMember = rideCardService.getByOrderNO(rideCardDTO.getOrderNO());
        Integer useCount = Optional.ofNullable(userMember.getUseCount()).orElse(0);
        if (userMember.getIsValid() == 0) {
            throw new BaseException(SystemCodeEnum.RIDECARDUSER_IS_INVALID);
        }
        if (useCount == 0 && rideCardDTO.getType() == 0) {
            throw new BaseException(SystemCodeEnum.RIDECARD_EDIT_ERROR);
        }
        HashMap<String, Object> map = new HashMap<>();
        RideCard rideCard = rideCardService.getById(rideCardDTO.getRideCardId());
        map.put("userType", rideCard.getUserType());
        rideCardService.EdituserRideCard(map, rideCardDTO.getUserId(), rideCardDTO.getRideCardId(),
                rideCardDTO.getType(), rideCardDTO.getOrderNO(), useCount, rideCardDTO.getMaxRide());
        if (Objects.equals(rideCard.getUserType(), 1) && rideCardDTO.getType() < 2) {
            if (rideCard.getMaxRide() - userMember.getCurrentNum() <= 0) {
                throw new BaseException("可使用次数超过每日上限,不可添加使用次数");
            }
            int updateCount = rideCardDTO.getType() == 1 ? userMember.getCurrentNum() + 1 : Math.max(userMember.getCurrentNum() - 1, 0);
            rideCardService.updateCurrentNumByOrderNo(userMember.getOrderNO(), updateCount);
        }
        return Result.success();
    }
}
